//
//  BlockViewController.swift
//  自定义tabbar和navigationbar
//
//  Created by lete on 2020/4/24.
//  Copyright © 2020 lete. All rights reserved.
//

import UIKit

//    1. 在B中声明一个block
typealias TestBlock = (String)->()

class BlockViewController: UIViewController, BDelegate {
    
    
    var name: NSString?
    
    //    2. 持有一个block变量
    var blo: TestBlock?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        view.backgroundColor = UIColor.white
        
        initLabel()
        
        
        
    }
    
    func initLabel() {
        //设置标签x坐标：30，y坐标：30，长：100，宽：30
        let label = UILabel(frame: CGRect(x: 130, y: 230, width: 190, height: 80))
        //设置label的文本
        label.text = name! as String
        
        label.textColor = UIColor.white//设置文字的颜色
        label.backgroundColor = UIColor.blue//设置背景颜色
        label.font = UIFont(name:"Zapfino", size:18)//设置字体
        label.textAlignment = .center//居中对齐
        label.shadowColor = UIColor.gray  //灰色阴影
        label.shadowOffset = CGSize(width:1.5, height:1.5)  //阴影的偏移量
        label.lineBreakMode = .byTruncatingTail  //隐藏尾部并显示省略号
        //当文字超出标签宽度时，自动调整文字大小，使其不被截断
        //        label.adjustsFontSizeToFitWidth = true
        //显示两行文字（默认只显示一行，设为0表示没有行数限制）
        label.numberOfLines = 2
        //将label添加到view的子视图中
        
        self.view.addSubview(label)
    }
    
    
    //block 回调值
    @IBAction func sendBackMsgBtn(_ sender: UIButton) {
        
        //        3. 调用
        self.blo?("It is block test")
        
        self.navigationController?.popViewController(animated: true)
        
    }
    
    
    //代理回调值
    @IBAction func delegatePage(_ sender: UIButton) {
        
        ////创建一个页面(Storyboard)
        let sb = UIStoryboard(name: "MsgStoryboard", bundle: nil)
        let delegateVC = sb.instantiateViewController(withIdentifier: "DelegateViewController") as! DelegateViewController
        
        //遵守代理
        delegateVC.delegate = self
        //跳转
        self.navigationController?.pushViewController(delegateVC, animated: true)
        
    }
    
    //代理实现方法
    func testDelegate(str: String) -> Void {
        print("test delegate---\\(str)")
    }
    
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}
